<?php

/**
 * 
 */
namespace backend\controllers;

use bc\controller\BaseController;
use yii;
use backend\service\system\UserService;

/**
 * Index controller
 */
class IndexController extends BaseController
{
	
	/**
	 * 登录
	 */
	public function actionLogin()
	{
		$this->layout = false;
		$this->getView ()->title = '登录';
		if ($this->getRequest ()->isPost)
		{
		    //获取账号和密码的信息
			$userName = $this->post ( 'user_name' );
			$password = $this->post ( 'pwd' );
			//
			$service = new UserService ();
			$result = $service->auth ( $userName, $password );
			
			if ($result)
			{
				// 页面跳转
				$this->redirect ( '/site/index' );
			}
			else
			{
				$msg = '用户名，密码错误!';
				return $this->render ( 'login', ['msg' => $msg] );
			}
			
		}
		else
		{
			return $this->render ( 'login' );
		}
	}
	public function actionLogout()
	{
		$session = new yii\web\Session ();
		$session->removeAll();
		$params = [ 
				'url' => '/index/login',
				'msg' => '退出成功!' 
		];
		$this->forward ( $params );
	}
	public function actionGoto()
	{
		$this->layout = false;
		$params = Yii::$app->getSession ()->getFlash ( 'goto' );
		Yii::$app->getSession ()->setFlash ( 'goto', null );
		
		$goto ['msg'] = '页面跳转中 ...';
		
		if (isset ( $params ['msg'] ))
			$goto ['msg'] = $params ['msg'];
		if (isset ( $params ['delay'] ))
		{
			$goto ['delay'] = $params ['delay'];
		}
		else
		{
			$goto ['delay'] = 10;
		}
		
		if (isset ( $params ['url'] ))
		{
			$goto ['url'] = $params ['url'];
		}
		else
		{
			$goto ['url'] = '/';
		}
		
		return $this->render ( 'goto', [ 
				'goto' => $goto 
		] );
	}
}
